package com.dingtalk.dingbotmgr.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dingtalk.dingbotmgr.model.Payment;
import com.dingtalk.dingbotmgr.model.PaymentCategory;
import com.dingtalk.dingbotmgr.model.Project;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Created by wyx on 2017/2/28.
 */
@Component("paymentAction")
@Scope("prototype")
public class PaymentAction extends BaseAction {

    private Payment payment;

    private PaymentCategory paymentCategory;

    private List<PaymentCategory> paymentCategoryList;

    private Project project;

    private Double remain, total;

    private Map<PaymentCategory, Double> remainMap;

    public String listPayment() {
        project = projectService.getEntityById(project.getId());
        paymentCategoryList = paymentCategoryService.listByProject(project);
        total = project.getFund();
        remain = project.getFund();
        remainMap = new HashMap<>();
        if (paymentCategoryList != null) {
            for (PaymentCategory paymentCategory : paymentCategoryList) {
                double pcTotal = paymentCategory.getFund();
                double pcRemain = pcTotal;
                for (Payment payment : paymentCategory.getPaymentList()) {
                    Double value = payment.getValue();
                    remain -= value;
                    pcRemain -= value;
                }
                remainMap.put(paymentCategory, pcRemain);
            }
        }
        return SUCCESS;
    }

    public String savePaymentCategory() {
        paymentCategoryService.saveOrUpdate(paymentCategory);
        project = paymentCategory.getProject();
        return SUCCESS;
    }

    public String deletePaymentCategory() {
        paymentCategory = paymentCategoryService.getEntityById(paymentCategory.getId());
        project = paymentCategory.getProject();
        paymentCategoryService.delete(paymentCategory);
        return SUCCESS;
    }

    public String savePayment() {
        paymentService.saveOrUpdate(payment);
        paymentCategory = paymentCategoryService.getEntityById(payment.getPaymentCategory().getId());
        project = paymentCategory.getProject();
        return SUCCESS;
    }

    public String deletePayment() {
        payment = paymentService.getEntityById(payment.getId());
        paymentCategory = paymentCategoryService.getEntityById(payment.getPaymentCategory().getId());
        project = paymentCategory.getProject();
        paymentService.delete(payment);
        return SUCCESS;
    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public List<PaymentCategory> getPaymentCategoryList() {
        return paymentCategoryList;
    }

    public void setPaymentCategoryList(List<PaymentCategory> paymentCategoryList) {
        this.paymentCategoryList = paymentCategoryList;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public Double getRemain() {
        return remain;
    }

    public void setRemain(Double remain) {
        this.remain = remain;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public PaymentCategory getPaymentCategory() {
        return paymentCategory;
    }

    public void setPaymentCategory(PaymentCategory paymentCategory) {
        this.paymentCategory = paymentCategory;
    }

    public Map<PaymentCategory, Double> getRemainMap() {
        return remainMap;
    }

    public void setRemainMap(Map<PaymentCategory, Double> remainMap) {
        this.remainMap = remainMap;
    }
}
